﻿ 
using System.Collections.Generic;
using System.Linq; 
using NTTDATA.BEENChina.ResultCertification.Config;
using NTTDATA.BEENChina.ResultCertification.DataStore;
using NTTDATA.BEENChina.ResultCertification.EntryModel;
using NTTDATA.BEENChina.ResultCertification.Logic;
using NTTDATA.BEENChina.ResultCertification.Split;
using NTTDATA.BEENChina.ResultCertification.Util;

namespace NTTDATA.BEENChina.ResultCertification.WcfServiceLibrary
{
    public class ServiceEntry : IServiceEntry
    {

        

        /// <summary>
        /// 得到检验数据（未做成job）的日期
        /// </summary>
        /// <returns></returns>
        public List<string> GetDistinctOridReceiveDates()
        {
            IMakeJob makeJob = new MakeJobClientAccess();

            return makeJob.GetDistinctOridReceiveDates();
        }

        /// <summary>
        /// 做成job
        /// </summary>
        /// <param name="oridReceiveDate"></param>
        public void MakeJob(string oridReceiveDate)
        {
            VerfityJob verfityJob = new VerfityJob();
            verfityJob.MakeJob(oridReceiveDate);
        }

        /// <summary>
        /// 用户帐号 密码验证
        /// </summary>
        /// <param name="id"></param>
        /// <param name="pass"></param>
        /// <param name="ipAddress"> </param>
        /// <returns></returns>
        public bool VerfityOperator(string id, string pass, string ipAddress)
        {
            //todo save ip address
           IEntryClient entryClient = new EntryClientAccess();
           bool bVerfity =  entryClient.VerfityOperator(id, pass);
           if (bVerfity)
           {
               entryClient.SetClient(id, ipAddress);
           }
            return bVerfity;
        }

        public int LogOut(string id)
        {
            IEntryClient entryClient = new EntryClientAccess();
          
            
            return entryClient.SetClient(id,string.Empty);
           
           
        }

        /// <summary>
        /// 以做成 未录入的job 中的 oridreceivedate
        /// </summary>
        /// <returns></returns>
        public List<string> GetDistinctOrderReceiveDates()
        {
            IEntryClient entryClient = new EntryClientAccess();
            return entryClient.GetDistinctOrderReceiveDates();
        }
         

        /// <summary>
        /// 得到job列表
        /// </summary>
        /// <returns></returns>
        public List<VerfityJobListItem> GetJobList(string orderReceiveDate, string categoryType)
        {
            IEntryClient entryClient = new EntryClientAccess();

            return entryClient.GetJobList(orderReceiveDate,categoryType) ;
        }

/*       
 *      /// <summary>
        /// 下载job 根据 verfityCategoryID
        /// </summary>
        /// <param name="opid"></param>
        /// <param name="verfityItemID"></param>
        /// <param name="orderReceiveDate"> </param>
        /// <returns></returns>
        public VerfityJobItem DownLoadJob(string opid, string verfityItemID, string orderReceiveDate)
        {
            IEntryClient entryClient = new EntryClientAccess();

            //return entryClient.DownLoadJob(opid, verfityItemID, orderReceiveDate);

            var verfityJobItem = entryClient.DownLoadJob(opid, verfityItemID, orderReceiveDate);

            if (verfityJobItem != null)
            {
                //图像下载相关参数设定
                verfityJobItem = Util.ImageDownloadHelper.GetImageData(verfityJobItem);

                var childImages = verfityJobItem.ChildImage;

                if (childImages != null)
                {
                    foreach (var childImage in childImages)
                    {
                        foreach (var opEntryResult in childImage.OperatorEntryResults)
                        {
                            opEntryResult.SplitEntryResult = GetSplitResult(childImage.CategorySetting, opEntryResult.EntryResult);
                        }
                        
                    }
                }
            }

            return verfityJobItem;
        }*/

        public VerfityJobItem DownLoadJob(string opid, string verfityItemID, string categoryID, string orderReceiveDate)
        {
            IEntryClient entryClient = new EntryClientAccess();

            //return entryClient.DownLoadJob(opid, verfityItemID, orderReceiveDate);

            var verfityJobItem = entryClient.DownLoadJob(opid, verfityItemID, categoryID, orderReceiveDate);

            if (verfityJobItem != null)
            {
                //图像下载相关参数设定
                verfityJobItem = new Util.ImageDownloadHelper().GetImageData(verfityJobItem, orderReceiveDate);

                var childImages = verfityJobItem.ChildImage;

                if (childImages != null)
                {
                    foreach (var childImage in childImages)
                    {
                        foreach (var opEntryResult in childImage.OperatorEntryResults)
                        {
                            opEntryResult.SplitEntryResult = GetSplitResult(childImage.CategorySetting, opEntryResult.EntryResult);
                        }

                    }
                }
            }

            return verfityJobItem;
        }

        /// <summary>
        /// 上传job
        /// </summary>
        /// <param name="opid"></param>
        /// <param name="verfityJobItem"></param>
        /// <returns></returns>
        public int UpLoadJob(string opid, VerfityJobItem verfityJobItem)
        {
            //IEntryClient entryClient = new EntryClientAccess();
            //return  entryClient.UpLoadJob(opid, verfityJobItem);
            return new VerfityJob().UpLoadJob(opid, verfityJobItem);
        }

        /// <summary>
        /// 取消job
        /// </summary>
        /// <param name="opid"></param>
        /// <param name="verfityJobID"> </param>
        /// <param name="CategoryID"> </param>
        /// <returns></returns>
        public int CancelJob(string opid,  string verfityJobID,string CategoryID)
        {
            IEntryClient entryClient = new EntryClientAccess();
            return entryClient.CancelJob(opid, verfityJobID, CategoryID);
        }

        public string GetImageBackupDatabaseConnstring()
        {
           return ConnectHelper.GetImageBackupDatabaseConnstring();
        }

        public string GetImageBackupServiceAddress()
        {
            return ConnectHelper.GetImageBackupServiceAddress();
        }

        public string GetImageFolder()
        {
            return ConnectHelper.GetImageFolder();
        }

        private List<List<string>> GetSplitResult(CategoryConfig categoryConfig,string entryResult)
        {
      
            Split.ISplit split = new SplitResult( categoryConfig );

           return split.GetSplitResult(entryResult).ToList();
           
        }
    }
}
